package com.varian.service.exception.handler;

import cn.hutool.core.util.StrUtil;
import com.varian.tool.contant.ResultConstant;
import com.varian.tool.result.IR;
import com.varian.tool.result.R;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * @author ben
 * @since 2024/6/28
 */
public class NoHandlerFoundExceptionHandler implements ExceptionHandlerStrategy {
    @Override
    public Class<? extends Exception> getExceptionClass() {
        return NoHandlerFoundException.class;
    }

    @Override
    public IR handle(Exception exception, HttpServletResponse response) {
        NoHandlerFoundException noHandlerFoundException = (NoHandlerFoundException) exception;
        String message = StrUtil.format("Not Found: {}", noHandlerFoundException.getRequestURL());
        return R.result(ResultConstant.NOT_FOUND, ResultConstant.FAIL_CODE, message);
    }
}
